//
//  MineFeedbackOnTheIssueQADetailViewController.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/23.
//

import UIKit

class MineFeedbackOnTheIssueQADetailViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
    }
    private func setupUI() {
        view.addSubviews(titleLabel, textView, addImageLabel, addImageBgView, emailTipLabel, emailTextField, submitButton)
        textView.addSubview(textViewPlaceholderLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(32)
            make.left.equalTo(14)
            make.height.equalTo(21)
        }
        textView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.height.equalTo(156)
        }
        textViewPlaceholderLabel.snp.makeConstraints { make in
            make.top.equalTo(8)
            make.left.equalTo(8)
            make.width.equalTo(ScreenSize.width - 48)
        }
        addImageLabel.snp.makeConstraints { make in
            make.top.equalTo(textView.snp.bottom).offset(20)
            make.height.equalTo(31)
            make.left.equalTo(14)
        }
        addImageBgView.snp.makeConstraints { make in
            make.top.equalTo(addImageLabel.snp.bottom).offset(5)
            make.left.equalTo(12)
            make.right.equalTo(-12)
        }
        emailTipLabel.snp.makeConstraints { make in
            make.top.equalTo(addImageBgView.snp.bottom).offset(25)
            make.left.equalTo(14)
            make.height.equalTo(32)
        }
        emailTextField.snp.makeConstraints { make in
            make.top.equalTo(emailTipLabel.snp.bottom)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.height.equalTo(44)
        }
        submitButton.snp.makeConstraints { make in
            make.top.equalTo(emailTextField.snp.bottom).offset(30)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.height.equalTo(50)
        }
        addImages()
    }
    private func addImages() {
        addImageBgView.addSubview(addImageButton)
        addImageButton.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.width.height.equalTo(90)
            make.bottom.equalToSuperview()
        }
    }
    //=================================================================
    //                           事件处理
    //=================================================================
    // MARK: - 事件处理
    @objc private func submitButtonAction() {
        
        Toast.with(message: "提交反馈成功").showOnWindow()
    }
    @objc private func addImageButtonAction() {
        Toast.with(message: "选择图片").showOnWindow()
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "问题描述(至少10个字)"
        label.textColor = .lxs.hex("#A6A6A6")
        label.font = .lxs.font(size: 14)
        return label
    }()
    private lazy var textView: UITextView = {
        let textView = UITextView()
        textView.text = ""
        textView.textColor = .lxs.hex("#FFFFFF")
        textView.font = .lxs.font(size: 12)
        textView.contentInset = UIEdgeInsets(top: 14, left: 12, bottom: 14, right: 12)
        textView.backgroundColor = .lxs.hex("#152443")
        textView.delegate = self
        textView.layer.cornerRadius = 10
        textView.layer.masksToBounds = true
        return textView
    }()
    private lazy var textViewPlaceholderLabel: UILabel = {
        let label = UILabel()
        label.text = "请输入不少于10个字的描述;建议您在主界面找到客服,直接联系客服为您高效解决"
        label.textColor = .lxs.hex("#6C6D6E")
        label.font = .lxs.font(size: 12)
        label.numberOfLines = 0
        return label
    }()
    private lazy var addImageLabel: UILabel = {
        let label = UILabel()
        label.text = "添加图片0/4(选填)"
        label.textColor = .lxs.hex("#A6A6A6")
        label.font = .lxs.font(size: 14)
        return label
    }()
    private lazy var addImageBgView: UIView = {
        let bgView = UIView(frame: .zero)
        
        return bgView
    }()
    private lazy var emailTipLabel: UILabel = {
        let label = UILabel()
        label.text = "联系邮箱(选填)"
        label.textColor = .lxs.hex("#A6A6A6")
        label.font = .lxs.font(size: 14)
        return label
    }()
    
    private lazy var emailTextField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = NSAttributedString(string: "填写您的邮箱或其他联系方式方便我们回复您", attributes: [NSAttributedString.Key.foregroundColor : UIColor.lxs.hex("#6C6D6E"), NSAttributedString.Key.font: UIFont.lxs.font(size: 12)])
        textField.textColor = .lxs.hex("#FFFFFF")
        textField.font = .lxs.font(size: 12)
        textField.layer.cornerRadius = 10
        textField.layer.masksToBounds = true
        textField.backgroundColor = .lxs.hex("#152443")
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 60))
        textField.leftView = leftView
        textField.leftViewMode = .always
        textField.rightView = leftView;
        textField.rightViewMode = .always
        return textField
    }()
    private lazy var submitButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setBackgroundImage(UIImage(named: "login_bg_img"), for: .normal)
        button.setTitle("提交", for: .normal)
        button.setTitle("提交", for: .selected)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .normal)
        button.setTitleColor(.lxs.hex("#F7FAFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 16)
        button.addTarget(self, action: #selector(submitButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var addImageButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "add_img_img"), for: .normal)
        button.setImage(UIImage(named: "add_img_img"), for: .selected)
        button.setBackgroundImage(UIImage(named: "add_img_bg_img"), for: .normal)
        button.setBackgroundImage(UIImage(named: "add_img_bg_img"), for: .selected)
        button.addTarget(self, action: #selector(addImageButtonAction), for: .touchUpInside)
        return button
    }()
}

extension MineFeedbackOnTheIssueQADetailViewController: UITextViewDelegate {
    func textViewDidChange(_ textView: UITextView) {
        textViewPlaceholderLabel.isHidden = !textView.text.isEmpty
    }
}
